package com.tledu.jdbc;

import java.sql.*;

public class Jdbc4 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // jdbc的流程也比较固定
        // 1. 装载依赖
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 2. 创建数据库的连接
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://127.0.0.1:3306/jingmao20",
                "root",
                "root"
        );
        // 查询操作
        String sql = "select * from student where school = ?";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);

        // 设置参数
        preparedStatement.setString(1,"河北经贸大学");
        // 执行sql
        ResultSet resultSet = preparedStatement.executeQuery();
        // resultSet 就是查询返回的结果
        // next（）判断有没有下一个
        while (resultSet.next()) {
            // 执行next方法之后，这里就相当于操作下一个的值
            System.out.println(resultSet.getString("name"));
            System.out.println(resultSet.getInt("age"));
            System.out.println(resultSet.getString("school"));
            System.out.println("----------------------------------------");
        }


        preparedStatement.close();
        conn.close();
    }
}
